package com.kaifamiao.www.demo.do0811;

import java.util.*;

public class MyList implements List {
        //内部容器
        private Object[] objs;
        //元素的个数
        private int count;
        //负载因子
        private float capacity;

        public MyList(){
            this(10);
        }

        public MyList(int size){
            if(size >=0){
                objs =new Object[size];
            }else{
                objs = new Object[10];
            }
            capacity = 0.8F;
        }
        public MyList(Object[] objs){
            this.objs = objs;
            count = objs.length;
            capacity =0.8F;
        }


        @Override
        public int size() {
            return count;
        }

        @Override
        public boolean isEmpty() {
            return count == 0;
        }
        //是否包含
        @Override
        public boolean contains(Object o) {

            return indexOf(o) >= 0;
        }


        @Override
        public Iterator iterator() {

            return new MyListIerator();
        }

    /**
     * 自定义迭代器
     *   迭代 MyList 元素
     * @return
     */

    private class MyListIerator implements Iterator {
        int index = -1;

        /**
         * 判断是否有下一个元素
         * @return
         */
        @Override
        public boolean hasNext() {
            return index < count -1;
        }

        /**
         * 取下一个元素
         * @return
         */
        @Override
        public Object next() {
            if (hasNext()){
                return objs[++ index];
            }
            return null;
        }
    }

    @Override
        public Object[] toArray() {
            return Arrays.copyOf(objs,count);
        }

    /**
     * 添加元素
     * @param o  添加的元素
     * @return  TRUE 添加成功
     */

    @Override
        public boolean add(Object o) {
            if(count >=objs.length*capacity){
                //扩容
                grow();
            }
            objs[count] = o;
            count++;
            return true;
        }
        private void grow(){
        int oldSize = objs.length;
        //1.5倍
        int newSize = oldSize + (oldSize >>1);
        //将原来的数组复制到新的容器中
            objs = Arrays.copyOf(objs,newSize);
        }


        @Override
        public boolean remove(Object o) {
            int index = indexOf(o);
            if(index >= 0){
                System.arraycopy(objs,index+1,objs,index,count-index-1);
                objs[--count]=null;
                return true;
            }
            return false;
        }


        @Override
        public boolean addAll(Collection c) {
            // 可以不实现
            return false;
        }

        @Override
        public boolean addAll(int index, Collection c) {
            // 可以不实现
            return false;
        }

        @Override
        public void clear() {
        Arrays.fill(objs,null);
        count = 0;
        }

        @Override
        public Object get(int index) {
            checkIndex(index);
//            if (index >= 0 && index < count){
            return objs[index];
//            }

        }
        private void checkIndex(int index){
        if (index < 0||index > count){
            throw new IndexOutOfBoundsException("下标不合法"+index);
        }
        }

        @Override
        public Object set(int index, Object element) {
            if (index >= 0 && index < count){
                Object old = objs[index];
                objs[index] = element;
                return old;
            }
            return null;
        }
        //将元素添加到下标位置
        @Override
        public void add(int index, Object element) {
            //判断空间是否够   不够扩容
            if (count + 1 > objs.length * capacity){
                grow();
            }
            System.arraycopy(objs,index,objs,index+1,count-index-1);
            objs[index] = element;
            count++;
        }
        //删除指定下标位元素
        @Override
        public Object remove(int index) {
            if (index >= 0 && index < count){
                Object old = objs[index];
                System.arraycopy(objs,index+1,objs,index,count-index-1);
                return old;
            }else{
                throw new IndexOutOfBoundsException("下表不合法");
            }
        }

        @Override
        public int indexOf(Object o) {
        for(int i = 0;i<count;i++){
            if(objs[i] == null&& o == null){
                return i;
            }else if(objs[i]!=null&&objs[i].equals(o)){
                return i;
            }
        }
            return -1;
        }

        @Override
        public int lastIndexOf(Object o) {
        for (int i = count-1;i>=0;i--){
            if (objs[i] == null&& o == null){
                return i;
            }else if(objs[i]!=null&&objs[i].equals(o)){
                return i;
            }
        }
            return -1;
        }



        @Override
        public ListIterator listIterator() {
            // 可以不实现
            return null;
        }

        @Override
        public ListIterator listIterator(int index) {
            // 可以不实现
            return null;
        }

        @Override
        public MyList subList(int fromIndex, int toIndex) {
            if(fromIndex >= 0 &&fromIndex<count&&fromIndex<=toIndex){
                Object[] objects = new Object[toIndex-fromIndex];
                System.arraycopy(objs,fromIndex,objects,0,objects.length);
                return new MyList(objects);
            }else{
                throw new IndexOutOfBoundsException("下表不合法"+fromIndex+",toIndex"+toIndex);
            }
        }

        @Override
        public boolean retainAll(Collection c) {
            // 可以不实现
            return false;
        }

        @Override
        public boolean removeAll(Collection c) {
            // 可以不实现
            return false;
        }

        @Override
        public boolean containsAll(Collection c) {
            // 可以不实现
            return false;
        }

        @Override
        public Object[] toArray(Object[] a) {
            //不实现
            return new Object[0];
        }
        @Override
        public String toString(){
        StringBuilder sb = new StringBuilder("MyList[");//StringBuilder可变的字符序列
        for(int i = 0;i<count;i++){
            sb.append(objs[i]+",");
        }
        return sb.substring(0,sb.length()-1) + "]";//substring子串，子链。
        }

}
